package com.sfdk.graphics.tooltip
{
import com.sfdk.graphics.core.SpriteEx;
import com.sfdk.graphics.core.TextFieldEx;

import flash.events.Event;
import flash.events.TimerEvent;
import flash.geom.Point;
import flash.utils.Timer;

public class ToolTipSprite extends SpriteEx
	{
		private var tooltipInfo_ : ToolTipInfo;
		private var delayTimer_ : Timer;
		private var fadeTimer_ : Timer;
		private var alphaIncrement_ : Number;
		private var toolTipText_ : TextFieldEx;
		
		public function ToolTipSprite(tooltipInfo : ToolTipInfo)
		{
			super();
			tooltipInfo_ = tooltipInfo;
			
			// Timer pour retarder l'affichage
			delayTimer_ = new Timer(tooltipInfo.getDelay() * 1000, 1); // timer en ms
			delayTimer_.addEventListener(TimerEvent.TIMER_COMPLETE,
										 function() : void {fadeTimer_.start()}, false, 0, true
										 );
			
			// Timer pour gérer le fade in a 60 frame / sec
			var fadeNumber : int = int(Math.floor(( tooltipInfo.getFade() / 60)));
			fadeTimer_ = new Timer(60, (tooltipInfo.getFade() / 60));
			alphaIncrement_ = 1 / fadeNumber;
			fadeTimer_.addEventListener(TimerEvent.TIMER, 
										function() : void {alpha += alphaIncrement_; toolTipText_.alpha += alphaIncrement_}, false, 0, true
										);
			
			addEventListener(Event.ADDED_TO_STAGE,
							 function() : void {delayTimer_.start()}, false, 0, true
							 );
			addEventListener(Event.REMOVED_FROM_STAGE, killTimers, false, 0, true);
		}
	
		public function initLabel() : void
		{
			var toolTipPos : Point = tooltipInfo_.getToolTipPosition();
			// Initiliase du texte
			toolTipText_ = new TextFieldEx("Arial-White-12");			
			toolTipText_.text = tooltipInfo_.getValue();
			toolTipText_.x = 0;
			toolTipText_.y = 0;
			
			// On dessine la zone
			graphics.beginFill(0x695547, 0.8);
			graphics.drawRoundRect(-5, 0, toolTipText_.textWidth + 14, toolTipText_.textHeight + 4, 10, 10);
			graphics.endFill();
			
			// On teste si on dépasse pas
			this.x = toolTipPos.x - width - 15;			
			this.y = toolTipPos.y; 
			
			// On ajoute la zone de texte	
			this.addChild(toolTipText_);
			
			// On interdit la souris sur ces composants
			mouseEnabled=false;
			mouseChildren=false;
			alpha = 0;
			toolTipText_.alpha = 0;
		}
		
		private function killTimers(e : Event) : void
		{
			// On arrete les timer pour pas qu'ils fassent des betises encore
			fadeTimer_.stop();
			delayTimer_.stop();
			fadeTimer_ = null;
			delayTimer_ = null;
			removeAllChildren();
		}
	}
}